<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Master extends MX_Controller {


	function __construct()
	{
		parent::__construct();
		$this->load->model('dump/master_model');
	}

	function index(){
		
		print "Complete...";
	}
	
	
	
	function create_slug(){
		$sql = "select name from location";
		$result = $this->db->query($sql)->result();
		foreach ($result as $result){
			$u = url_title($result->name.'-'.'yellowpage');
			$s = "update location set slug = $u where id = $result->id";
			$this->db->query($s);
		}
		print "Complete...";
	}

	function import(){

		$error = 0;
		$postID=0;

		$sql = "select * from dump where status = 0 limit 0,50000";
		$result = $this->db->query($sql)->result();

		$this->db->trans_begin();

		foreach ($result as $result){
				
			$slug = fUrl($result->full_name.'-i'.$result->id);
			$data = array(
					'name'		=> $result->full_name,
					'degree'		=> $result->title,
					'gender'		=> $result->gender,
					'address1'		=> $result->address1,
					'address2'		=> $result->address2,
						
					'zip'		=> $result->zip,
					'phone'		=> $result->phone,
					'fax'		=> $result->fax,
					'email'		=> strtolower($result->email),
					'website'		=> $result->website,
					'since'		=> $result->since,
					'education'		=> $result->education,
						
					'slug'		=>  $slug,
					'updated_on'=> date('Y-m-d H:i:s'),
			);

			print $result->id."<br>";
			
			$id = $this->db->insert('post',$data);
			if($id == 0){
				$error = 1;
			}
				
			//--GET POST ID --//
			if($this->_getID($slug) != 0 ){
				$postID= $this->_getID($slug);
			}else{
				$error = 1;
			}
				
			//--HOSPITALS --//
			if($result->hospital != ''){
				$data = array(
						'post_id'		=> $postID,
						'detail'		=>  $result->hospital,
				);

				$id = $this->db->insert('doc_hospitals',$data);
				if($id == 0){
					$error = 1;
				}
			}
				
			//--CLINICS --//
			if($result->clinic != ''){
				$data = array(
						'post_id'		=> $postID,
						'detail'		=>  $result->clinic,
				);
					
				$id = $this->db->insert('doc_clinics',$data);
				if($id == 0){
					$error = 1;
				}
			}
				
			//--SPECIALITY --//
			$sp1 = ($result->speciality ? $result->speciality : null);
			
				
			if($sp1){
				if($this->_getCategory($sp1) != 0){
					$data = array(
							'post_id'		=> $postID,
							'term'		=>  'category',
							'caption'	=> 'Speciality',
							'value'		=> $this->_getCategory($sp1),
					);
					$id = $this->db->insert('term',$data);
					if($id == 0){
						$error = 1;
					}
				}
			}
			
			//--COUNTRY --//
			$data = array(
					'post_id'		=> $postID,
					'term'		=>  'country',
					'caption'	=> 'Country',
					'value'		=> 1,
			);
			$id = $this->db->insert('term',$data);
			if($id == 0){
				$error = 1;
			}
			
			//--STATE --//
			
			$data = array(
					'post_id'		=> $postID,
					'term'		=>  'state',
					'caption'	=> 'State',
					'value'		=> $this->_getState($result->state),
			);
			$id = $this->db->insert('term',$data);
			if($id == 0){
				$error = 1;
			}
			
			//--CiTY --//
				
			$data = array(
					'post_id'		=> $postID,
					'term'		=>  'city',
					'caption'	=> 'City',
					'value'		=> $this->_getCity($result->state,$result->city),
			);
			$id = $this->db->insert('term',$data);
			if($id == 0){
				$error = 1;
			}
			
			$updt = "Update dump set status = 1 where id = $result->id";
			$this->db->query($updt);
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->db->trans_rollback();
			}
			else
			{
				$this->db->trans_commit();
			}
			//print($postID); die();
		}
	}
	
	function _getCity($state='',$city=''){
	
		$sql = "select id from location where deep = 2 and prefix = '$state' and name = '$city' limit 0,1";
		$query = $this->db->query($sql);
		$result = $query->result();
	
		if($result){
			$id = $query->row()->id;
		}
		return $id;
	}
	
	function _getState($loc=''){
		
		$sql = "select id from location where deep = 1 and prefix = '$loc' limit 0,1";
		$query = $this->db->query($sql);
		$result = $query->result();
	
		if($result){
			$id = $query->row()->id;
		}
		return $id;
	}
	
	function _getCategory($cat=''){
		$id = 0;
		$sql = "select id from category where name = '$cat' limit 0,1";
		$query = $this->db->query($sql);
		$result = $query->result();

		if($result){
			$id = $query->row()->id;
		}
		return $id;
	}

	function _getID($slug=''){
		$id = 0;
		$sql = "select id from post where slug = '$slug' limit 0,1";
		$query = $this->db->query($sql);

		if($query){
			$id = $query->row()->id;
		}
		return $id;
	}

	
	
	
	
	
	
	
	
	
	
	
	function create_category(){
		$sql = "select unit from dump group by unit";
		print $sql;
		$result = $this->db->query($sql)->result();
		
		foreach ($result as $result){
			$sp = trim($result->unit);
			$sql = "select id from category where name = '$sp'";
			$result2 = $this->db->query($sql)->result();
			if(!$result2){
				$data = array(
						'name'			=> $result->name,
						'parent_id'		=>  1,
						'status'		=> 51,
						'slug'			=> fUrl($result->name),
				);
				$this->db->insert('category',$data);
				print $result->name."<br>";
			}
			
		}
	}





	function slug(){
		$sql = "Select id,name from location";
		$result = $this->db->query($sql)->result();
		foreach ($result as $result){
			$data = array(
					'slug'		=>  fUrl($result->name)
			);
			$this->db->where ('id',$result->id);
			$this->db->update('location',$data);
				
		}
		die('complete');
	}

	function category(){
		$sql = "Select name from category_tree where parent_id=8999206 group by (name)";
		$result = $this->db->query($sql)->result();
		foreach ($result as $result){
			$data = array(
					'parent_id' => 1,
					'name'		=>  $result->name,
			);
			$this->db->insert('category',$data);
				
		}
		die('complete');
	}

	function sync(){
		$config = array('table'=>'category');
		$this->load->library('mahana_hierarchy');
		$this->mahana_hierarchy->initialize($config);

		$this->mahana_hierarchy->resync();
	}

	function state(){
		$sql = "select * from state group by statename";
		$result = $this->db->query($sql)->result();
		foreach ($result as $result){
			$data = array(
					'parent_id' => 1,
					'name'		=>  $result->statename,
					'url'		=>  $result->statecode
			);

			print $data['name']."--"."</br>";
				
			$this->db->insert('location',$data);
		}
		die("COMPLETE");

	}

	function city(){

		$sql = "select id,url from location where url !='' group by url";
		$result = $this->db->query($sql)->result();

		foreach ($result as $result){
			$sql = "select * from city where statecode = '$result->url'";
			$city = $this->db->query($sql)->result();
				
			foreach ($city as $city){
				$data = array(
						'parent_id' => $result->id,
						'name'		=>  $city->cityname,
						'url'		=> $result->url
				);
				$this->db->insert('location',$data);
				//die();
			}
				
				

		}

		print_r($result); die();

		$sql = "select * from city where stateid = $id";
		$result = $this->db->query($sql)->result();



		foreach ($result as $result){

			print $result->stateid."--".$result->cityname ."-TO-".$bb; echo "</br>";
			$data = array(
					'parent_id' => $bb,
					'name'		=>  $result->cityname
			);

			$this->db->insert('location',$data);

		}
		die("COMPLETE");

	}


}
